﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;

namespace HdipCricketClub.Controllers
{ 
    public class ClubController : Controller
    {
        private CricketClubEntities db = new CricketClubEntities();

        ClubRepository clubRepository = new ClubRepository();

        //
        // GET: /Club/

        public ViewResult Index()
        {
            var clubs = clubRepository.FindAllClubs();
            return View(clubs);
        }

        //
        // GET: /Club/Details/5

        public ViewResult Details(int id)
        {
            Club club = clubRepository.GetClub(id);
            return View(club);
        }

        //
        // GET: /Club/Create

        public ActionResult Create()
        {
            MemberRepository memberRepository = new MemberRepository();
            var members = memberRepository.FindAllMembers();
            ViewBag.president = new SelectList(members, "member_id", "FirstAndLastName");
            ViewBag.chairman = new SelectList(members, "member_id", "FirstAndLastName");
            ViewBag.secretary = new SelectList(members, "member_id", "FirstAndLastName");
            ViewBag.fixtures_secretary = new SelectList(members, "member_id", "FirstAndLastName");
            ViewBag.county = new SelectList(db.Counties, "county_id", "name");
            return View();
        } 

        //
        // POST: /Club/Create

        [HttpPost]
        public ActionResult Create(Club club)
        {
            if (ModelState.IsValid)
            {   
                db.Clubs.AddObject(club);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.president = new SelectList(db.Members, "member_id", "FirstAndLastName", club.president);
            ViewBag.chairman = new SelectList(db.Members, "member_id", "FirstAndLastName", club.chairman);
            ViewBag.secretary = new SelectList(db.Members, "member_id", "FirstAndLastName", club.secretary);
            ViewBag.fixtures_secretary = new SelectList(db.Members, "member_id", "FirstAndLastName", club.fixtures_secretary);
            ViewBag.county = new SelectList(db.Counties, "county_id", "name", club.county);
            return View(club);
        }
        
        //
        // GET: /Club/Edit/5
 
        public ActionResult Edit(int id)
        {
            Club club = clubRepository.GetClub(id);
            ViewBag.president = new SelectList(db.Members, "member_id", "FirstAndLastName", club.president);
            ViewBag.chairman = new SelectList(db.Members, "member_id", "FirstAndLastName", club.chairman);
            ViewBag.secretary = new SelectList(db.Members, "member_id", "FirstAndLastName", club.secretary);
            ViewBag.fixtures_secretary = new SelectList(db.Members, "member_id", "FirstAndLastName", club.fixtures_secretary);
            ViewBag.county = new SelectList(db.Counties, "county_id", "name", club.county);
            return View(club);
        }

        //
        // POST: /Club/Edit/5

        [HttpPost]
        public ActionResult Edit(Club club)
        {
            if (ModelState.IsValid)
            {
                db.Clubs.Attach(club);
                db.ObjectStateManager.ChangeObjectState(club, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.president = new SelectList(db.Members, "member_id", "FirstAndLastName", club.president);
            ViewBag.chairman = new SelectList(db.Members, "member_id", "FirstAndLastName", club.chairman);
            ViewBag.secretary = new SelectList(db.Members, "member_id", "FirstAndLastName", club.secretary);
            ViewBag.fixtures_secretary = new SelectList(db.Members, "member_id", "FirstAndLastName", club.fixtures_secretary);
            ViewBag.county = new SelectList(db.Counties, "county_id", "name", club.county);
            return View(club);
        }

        //
        // GET: /Club/Delete/5
 
        public ActionResult Delete(int id)
        {
            Club club = clubRepository.GetClub(id);
            return View(club);
        }

        //
        // POST: /Club/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                Club club = clubRepository.GetClub(id);
                db.Clubs.DeleteObject(club);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                Club club = clubRepository.GetClub(id);
                return RedirectToAction("UpdateError", new { id = club.club_id });
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        //  GET:    Club/UpdateError/

        public ActionResult UpdateError(int id)
        {
            Club club = clubRepository.GetClub(id);

            return View(club);
        }


    }
}